package com.onpositive.notes.android.ui.elements;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.onpositive.businessdroids.model.IField;
import com.onpositive.businessdroids.model.impl.AbstractComputableField;
import com.onpositive.businessdroids.ui.dataview.DataViewFactory;
import com.onpositive.businessdroids.ui.dataview.StructuredDataView;
import com.onpositive.collections.ICollectionDelta;
import com.onpositive.collections.ICollectionMember;
import com.onpositive.notes.android.main.CommonResources;

public class ViewDeltaUI {
	
	private StructuredDataView sdv;
	private ICollectionDelta<?> delta = null ;
	
	public View createView( final Context context ){
		
		LinearLayout lt = new LinearLayout( context );
		lt.setOrientation( LinearLayout.VERTICAL ) ;
		
		while(true){
			if( !(context instanceof Activity) )
				break ;
			
			Bundle extras = ((Activity)context).getIntent().getExtras();
			if( extras == null )
				break ;
	        
			if( !extras.containsKey( CollectionHistoryUI.EXTRA_KEY_DELTA ) )
				break ;
			
			Object val = extras.get( CollectionHistoryUI.EXTRA_KEY_DELTA ) ;
			if(!(val instanceof ICollectionDelta ))
				break ;
			
			delta = (ICollectionDelta<?>) val ;			
			break ;
		}
		if( delta == null )
			return lt ;
		
		
		IField nameField = new AbstractComputableField() {

			@Override
			public Object getPropertyValue(Object object) {

				if (object == null)
					return "NULL";

				if (object instanceof ICollectionMember)
					return ((ICollectionMember<?>) object).getName() ;

				return "" ;
			}
		};

		IField valueField = new AbstractComputableField() {

			@Override
			public Object getPropertyValue(Object object) {

				if (object == null)
					return "NULL";

				if (object instanceof ICollectionMember) {
					ICollectionMember<?> member = (ICollectionMember<?>) object ;
					Object value = delta.getValue(member);					
					return value.toString() ;
				}
				return object.toString();
			}
		};
		
		IField[] fields = new IField[]{ nameField, valueField } ;
		
		sdv = DataViewFactory.createTable(
				context, delta.getAffectedMembers(), null, null, fields);
		
		sdv.setActionBarVisible(true) ;
		sdv.setHeaderVisible(true) ;
		sdv.setFooterVisible(true) ;

		
//		sdv.setOpenListener(new IOpenListener() {
//
//			@Override
//			public void openElement(View parent, View elementView, Object element, int pos)
//			{
//				ICollectionDelta<?> delta = (ICollectionDelta<?>) element;
//				Intent intent = new Intent( context, EditNoteActivity.class);
//				intent.putExtra(EXTRA_KEY_DELTA, delta);
//				context.startActivity(intent) ;
//			}
//		});
		
		Date date = delta.getDate() ;
		SimpleDateFormat sdf = new SimpleDateFormat( "dd,MMMM,yyyy HH:mm", CommonResources.defaultLocale ) ;		
		String dateString = sdf.format(date);
		
		TextView titleView = new TextView( context);
		titleView.setText( delta.getCollection().getName() + ", " + dateString ) ;
		lt.addView( titleView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT) ) ;
		
		lt.addView(sdv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		return lt ;	
	}

}
